﻿using DevExpress.Web.ASPxUploadControl;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using Utility1;

namespace eDream.admin
{
    public partial class wTintuc_new : System.Web.UI.UserControl
    {
        public string pathUrl = "";
        const string UploadDirectory = @"D:\inetpub\vhosts\msmart.vn\subdomains\tintuc\httpdocs\image\New\";
        protected void Page_Load(object sender, EventArgs e)
        {
            pathUrl = WebUtils.pathUrltintuc() + "/image/New/";
            if (!IsPostBack)
            {
                //LoadCBX();
                //radChitiet.Language = "vi";
                //CKEditorControl1.config.filebrowserImageBrowseUrl = "/ckfinder/ckfinder.html?type=Images";
                //CKEditorControl1.config.filebrowserImageUploadUrl = "/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Images";
                CKFinder.FileBrowser _FileBrowser = new CKFinder.FileBrowser();
                _FileBrowser.BasePath = "/ckfinder/";
              _FileBrowser.SetupCKEditor(radChitiet);
               

                Bindata();
            }
        }
        protected void uplImage_FileUploadComplete(object sender, FileUploadCompleteEventArgs e)
        {
            e.CallbackData = SavePostedFile(e.UploadedFile, 0, 337,300);
        }

        string SavePostedFile(UploadedFile uploadedFile, int index, int size,int size1)
        {
            if (uploadedFile.FileName == "")
                return string.Empty;

            // Random r = new Random(9999);
            // int n = r.Next(999999);
            string ThumbnailFileName = uploadedFile.FileName;
            string fileName = Path.Combine(UploadDirectory, ThumbnailFileName);
            //lbthongbao.Text = fileName;
            using (Image original = Image.FromStream(uploadedFile.FileContent))
            using (Image thumbnail = PhotoUtils.Inscribe1(original, size, size1))
            {
                //PhotoUtils.SaveToJpeg(thumbnail, fileName);
                ImageHandler h = new ImageHandler();
                h.Save(thumbnail, size, size, 90, fileName);
            }
            switch (index)
            {
                case 0:
                    Session["link0"] = ThumbnailFileName;
                    break;
                case 1:
                    Session["link1"] = ThumbnailFileName;
                    break;
               
            }
            Session.Timeout = 120;
            return ThumbnailFileName;
        }

        protected void uplImage1_FileUploadComplete(object sender, FileUploadCompleteEventArgs e)
        {
            e.CallbackData = SavePostedFile(e.UploadedFile, 1, 246, 184);
        }

        void createProduct()
        {
            DataClasses2DataContext dx = new DataClasses2DataContext();
            try
            {
                UserDB user_obj = (UserDB)Session[Global.SESS_USER];
                dx.Connection.Open();
                wTintuc_New p = new wTintuc_New();
               
                if (Session["link0"] != null)
                    p.bigPhoto = Session["link0"].ToString();
                if (Session["link1"] != null)
                    p.photo = Session["link1"].ToString();
                p.Content = radChitiet.Text;
                p.datepost = DateTime.Now;
                p.flag = true;
                p.title = txtTieude.Text;
                p.UserID=user_obj.ID;
                dx.wTintuc_News.InsertOnSubmit(p);
                dx.SubmitChanges();

                lbthongbao.ForeColor = Color.Red;
                lbthongbao.Text = "Sản phẩm đã được cập nhật";
                txtTieude.Text = "";
              
                radChitiet.Text = "";
                //  radHinhanh.Text = "";
                // radTinhnang.Text = "";
                //  radVideo.Text = "";

                Session.Remove("link0");
                Session.Remove("link1");
                Session.Remove("link2");
                Session.Remove("link3");
                Session.Remove("link4");
                Session.Remove("link5");
                Session.Remove("link6");
            }
            catch (Exception)
            {
                lbthongbao.ForeColor = Color.Red;
                lbthongbao.Text = "Đã xãy ra lỗi! vui lòng thử lại.";
            }
            finally { dx.Connection.Close(); }
        }

         void updateProduc()
        {
            DataClasses2DataContext dx = new DataClasses2DataContext();
            try
            {
                UserDB user_obj = (UserDB)Session[Global.SESS_USER];
               
                dx.Connection.Open();
                wTintuc_New p = dx.wTintuc_News.Single(l=>l.ID==int.Parse(hdfID.Value));
               
                if (Session["link0"] != null)
                    p.bigPhoto = Session["link0"].ToString();
                if (Session["link1"] != null)
                    p.photo = Session["link1"].ToString();
                p.Content = radChitiet.Text;
                p.datepost = DateTime.Now;
                p.flag = true;
                p.title = txtTieude.Text;
                p.UserID=user_obj.ID;
               
                dx.SubmitChanges();

                lbthongbao.ForeColor = Color.Red;
                lbthongbao.Text = "Sản phẩm đã được cập nhật";
                txtTieude.Text = "";
              
                radChitiet.Text = "";
                //  radHinhanh.Text = "";
                // radTinhnang.Text = "";
                //  radVideo.Text = "";

                Session.Remove("link0");
                Session.Remove("link1");
                Session.Remove("link2");
                Session.Remove("link3");
                Session.Remove("link4");
                Session.Remove("link5");
                Session.Remove("link6");
            }
            catch (Exception)
            {
                lbthongbao.ForeColor = Color.Red;
                lbthongbao.Text = "Đã xãy ra lỗi! vui lòng thử lại.";
            }
            finally { dx.Connection.Close(); }
        }

         void Bindata()
         {
             DataClasses2DataContext dx = new DataClasses2DataContext();

             try
             {
                 dx.Connection.Open();
                 if (Request.QueryString["k"] != null)
                 {
                     hdfID.Value = Request.QueryString["k"];
                     var list = from k in dx.wTintuc_News
                                where k.ID==int.Parse(hdfID.Value)
                                select k;
                     foreach (wTintuc_New i in list)
                     {
                         txtTieude.Text = i.title;
                         radChitiet.Text = i.Content;

                     }
                 }
             }
             catch (Exception)
             {
                 
             }
             finally { dx.Connection.Close(); }
         }
        protected void btnOk_Click(object sender, EventArgs e)
        {
            if (hdfID.Value.ToString() != "")
                updateProduc();
            else
                createProduct();
        }
    }
}